#region Check Admin
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
 
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
 
# Check to see if we are currently running "as Administrator"
if (!$myWindowsPrincipal.IsInRole($adminRole)){
   # We are not running "as Administrator" - so alert user
   Write-Warning "The commands in the PeopleSoft module should be run as an administrator."
}
#endregion Check Admin

Get-ChildItem -Path $PSScriptRoot | Unblock-File
Get-ChildItem -Path $PSScriptRoot\*.ps1 | Foreach-Object{ . $_.FullName }

# --- Helper Functions ---

function Validate_TUXDIR{
    param ($TUXDIR)
    if(Test-Path "$TUXDIR\bin\tmadmin.exe"){
        return $true
    }else{
        throw "Please enter a valid path for the Tuxedo directory."
    }
}

function Validate_PS_CFG_HOME{
    param($PS_CFG_HOME)
    if(Test-Path "$PS_CFG_HOME\appserv\"){
        return $true
    }else{
        throw "Please enter a valid path for the PeopleSoft Configuration Home."
    }
}

function Validate_PS_HOME{
    param($PS_HOME)
    if(Test-Path "$PS_HOME\appserv\psadmin.exe"){
        return $true
    }else{
        throw "Please enter a valid path for the PeopleSoft Configuration Home."
    }
}

function Validate_Domain{
    param($PS_CFG_HOME,$ServerType,$Domain)

    switch ($ServerType)
    {
        'AppServer' {$testPath = "$PS_CFG_HOME\appserv\$DOMAIN"}
        'PrcsScheduler' {$testPath = "$PS_CFG_HOME\appserv\prcs\$DOMAIN"}
        Default {$testPath = "$PS_CFG_HOME\appserv\$DOMAIN"}
    }

    if(Test-Path $testPath){
        return $true
    }else{
        throw "Please enter a valid PeopleSoft $ServerType domain name."
    }
}

function Validate_NewDomain{
    param($PS_CFG_HOME,$ServerType,$Domain)

    switch ($ServerType)
    {
        'AppServer' {$testPath = "$PS_CFG_HOME\appserv\$DOMAIN"}
        'PrcsScheduler' {$testPath = "$PS_CFG_HOME\appserv\prcs\$DOMAIN"}
        Default {$testPath = "$PS_CFG_HOME\appserv\$DOMAIN"}
    }

    if(!(Test-Path $testPath)){
        return $true
    }else{
        throw "The domain $Domain is already a PeopleSoft $ServerType domain name."
    }
}

function Validate_Cfg_Path{
    param($Path)

    if(!(Test-Path $Path)){
        throw "$Path not found."
    }

    if(!(Get-CfgContent $Path)["Startup"]){
        throw "$Path is not a valid PeopleSoft configuration file."
    }

    return $true
}

function SanitizeInput
{
    param([String]$inputVal)
    if($inputVal -ne "")
    {
        $inputVal = $inputVal -replace "/","\"
        if($inputVal -match "[\s]")
        {
            $inputVal = """$inputVal"""
        }
    }
    else
    {
        $inputVal = "_____"
    }
    return $inputVal
}